Passed
Push — master ( 74c350...233e8c )
by Kevin Van
03:58 queued 12s
created

MatchesPreseason.tsx ➔ getDataB   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import React, { FunctionComponent, useEffect, useState } from "react"
2
import { graphql, useStaticQuery } from "gatsby"
3
4
import axios from "axios"
5
import LazyLoad from "react-lazyload"
6
import classNames from "classnames"
7
import Moment from "moment-timezone"
8
import "moment/locale/nl-be"
9
10
import { capitalizeFirstLetter, groupByMonth, mapPsdStatus, mapPsdStatusShort } from "../scripts/helper"
11
12
import "./MatchesPreseason.scss"
13
import Spinner from "./Spinner"
14
15
const MatchOverviewMatch: FunctionComponent<MatchesRowProps> = ({ match }: MatchesRowProps) => {
16
  const d = Moment.tz(match.date, `Europe/Brussels`)
17
  const matchPlayed =
18
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
19
    false
20
21
  return (
22
    <article>
23
      <main className="matches__calendar__main matches__calendar__preseason">
24
        <div className="matches__calendar__date matches__preseason__date">
25
          <span className="matches__calendar__date matches__preseason__date--date">{d.format(`DD`)}</span>
26
          <span className="matches__calendar__date matches__preseason__date--day">
27
            {capitalizeFirstLetter(d.format(`dddd`))}
28
          </span>
29
          <span className="matches__preseason__divider"> // </span>
30
          <span className="matches__calendar__date matches__preseason__date--day">{d.format(`HH:mm`)}</span>
31
        </div>
32
33
        <div className="matches__preseason__match">
34
          <div
35
            className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, {
36
              "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
37
            })}
38
          >
39
            {match.homeClub?.name} {match.homeTeamId === null || (match.homeTeamId === 1 ? `A` : `B`)}
40
            <LazyLoad debounce={false}>
41
              <img
42
                src={match.homeClub?.logo}
43
                alt={match.homeClub?.name}
44
                className="matches__calendar__logo matches__calendar__logo--home"
45
              />
46
            </LazyLoad>
47
          </div>
48
49
          <div className="matches__calendar__score">
50
            {match.status !== 0 && (
51
              <span title={mapPsdStatus(match.status) || ``}>{mapPsdStatusShort(match.status)}</span>
52
            )}
53
            {(match.status === 0 || match.status === null) && !matchPlayed && <span>-</span>}
54
            {matchPlayed && (
55
              <span>
56
                {match.goalsHomeTeam} - {match.goalsAwayTeam}
57
              </span>
58
            )}
59
          </div>
60
61
          <div
62
            className={classNames(`matches__calendar__team`, `matches__calendar__team--away`, {
63
              "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
64
            })}
65
          >
66
            <LazyLoad debounce={false}>
67
              <img
68
                src={match.awayClub?.logo}
69
                alt={match.awayClub?.name}
70
                className="matches__calendar__logo matches__calendar__logo--away"
71
              />
72
            </LazyLoad>
73
            {match.awayClub?.name} {match.awayTeamId === null || (match.awayTeamId === 1 ? `A` : `B`)}
74
          </div>
75
        </div>
76
        <div className="matches__calendar__type matches__preseason__type">{match.competitionType}</div>
77
      </main>
78
    </article>
79
  )
80
}
81
82
const MatchesOverview: FunctionComponent<MatchesProps> = () => {
83
  const [dataA, setDataA] = useState<Match[]>([])
84
  const [dataB, setDataB] = useState<Match[]>([])
85
86
  const {
87
    site: {
88
      siteMetadata: { kcvvPsdApi },
89
    },
90
  }: MatchesQueryData = useStaticQuery(graphql`
91
    {
92
      site {
93
        siteMetadata {
94
          kcvvPsdApi
95
        }
96
      }
97
    }
98
  `)
99
100
  Moment.locale(`nl-BE`)
101
102
  useEffect(() => {
103
    async function getDataA() {
104
      const response = await axios.get(`${kcvvPsdApi}/matches/1`)
105
      setDataA(response.data)
106
    }
107
    async function getDataB() {
108
      const response = await axios.get(`${kcvvPsdApi}/matches/2`)
109
      setDataB(response.data)
110
    }
111
    getDataA()
112
    getDataB()
113
  }, [])
114
115
  const data = groupByMonth(dataA.concat(dataB))
116
117
  return (
118
    <div className={`preseason__wrapper`}>
119
      {data.length > 0 || <Spinner />}
120
      {data.map(({ date, objects }, j: number) => (
121
        <>
122
          <h3>{date}</h3>
123
          {objects
124
            .sort((a: Match, b: Match) => a.timestamp - b.timestamp)
125
            .map((match: Match, i: number) => (
126
              <MatchOverviewMatch match={match} key={match.id} />
127
            ))}
128
        </>
129
      ))}
130
    </div>
131
  )
132
}
133
134
export default MatchesOverview
135